package com.ruding.disruptor.service;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruding.disruptor.init.SeriesData;
import com.ruding.disruptor.init.SeriesDataEventQueueHelper;
import com.ruding.disruptor.utils.UrlFilesToZipUtils;

@RestController
public class ReceiverServiceImpl {
	
	 private Logger logger = LoggerFactory.getLogger(ReceiverServiceImpl.class);

	@Autowired
	private SeriesDataEventQueueHelper queueHelper;


	@RequestMapping(value = "/receiverData/{num}")
	public String receiverData(@PathVariable("num") int num) {
		long startTime1 = System.currentTimeMillis();
		for (int i = 0; i < num; i++) {
			SeriesData data = new SeriesData(i+"",i+"数据",num,startTime1+"");
			queueHelper.tryPublishEvent(data);
		}
		long startTime2 = System.currentTimeMillis();
		logger.info("receiver data ==[{}] millisecond ==[{}]", num, startTime2 - startTime1);
		return "成功["+(startTime2 - startTime1)+"]";
	}
	
	@RequestMapping(value = "/filesDown")
	public String filesDown(HttpServletResponse response) {
		long startTime1 = System.currentTimeMillis();
		try {
            String filename = new String("xx.zip".getBytes("UTF-8"), "ISO8859-1");//控制文件名编码
            response.setContentType("application/force-download");// 设置强制下载不打开
            response.addHeader("Content-Disposition", "attachment;fileName=" + filename);// 设置文件名
            ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
            int idx = 1;
            List<String> urls = new ArrayList<String>();
            urls.add("https://img-blog.csdn.net/20180927154859492?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzMTg4NTYz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70");
            urls.add("https://img-blog.csdn.net/20180927154859492?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzMTg4NTYz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70");
            urls.add("https://img-blog.csdn.net/20180927154859492?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzMTg4NTYz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70");
            for (String oneFile : urls) {
                zos.putNextEntry(new ZipEntry("test"+idx+File.separator+"profile" + idx+".jpg"));
                byte[] bytes = UrlFilesToZipUtils.getImageFromURL(oneFile);
                zos.write(bytes, 0, bytes.length);
                zos.closeEntry();
                idx++;
            }
            zos.close();
        } catch (FileNotFoundException ex) {
            logger.error("FileNotFoundException", ex);
        } catch (Exception ex) {
            logger.error("Exception", ex);
        }
		long startTime2 = System.currentTimeMillis();
		logger.info("millisecond ==[{}]", startTime2 - startTime1);
		return "成功["+(startTime2 - startTime1)+"]";
	}
	
}
